package org.summerframework.core.util;

import org.summerframework.core.util.config.ConfigUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * url
 * <p>
 * {@link ConfigUtils#getProperty(String, String)}
 * url.conf.prefix 配置prefix开头, 默认值为 url.default
 * <p>
 * {@link #getUrl()}
 *
 * @author 石超
 * @version v1.0.0
 */
public final class UrlUtil {
    private static Map<String, String> CACHE_URL = new ConcurrentHashMap<>();

    private static final ConfigurationPropertyName URL = ConfigurationPropertyName.of("spring.url");
    private static final Bindable<Map<String, String>> STRING_STRING_MAP = Bindable
            .mapOf(String.class, String.class);
    private static final Pattern URL_NAME_PATTERN = Pattern.compile("\\{.*?}");

    public UrlUtil(ConfigurableEnvironment environment) {
        Binder binder = Binder.get(environment);
        CACHE_URL = binder.bind(URL, STRING_STRING_MAP)
                .orElseGet(Collections::emptyMap);

        //文件默认default
        if (!CACHE_URL.containsKey("file")) {
            CACHE_URL.put("file", CACHE_URL.get("default"));
        }
    }

    /**
     * 获取url 集合
     * <p>
     * 获取以 spring.url开头的数据
     * 当key值为 prefix.abc.def时, map的key为 abc_def
     *
     * @return url 集合
     */
    public static Map<String, String> getUrl() {
        return CACHE_URL;
    }

    /**
     * 获取url
     *
     * @param key key值
     * @return 地址
     */
    public static String getUrl(String key) {
        return getUrl(key, null);
    }

    /**
     * 获取url
     *
     * @param key        key值
     * @param defaultUrl 默认值
     * @return 地址
     */
    public static String getUrl(String key, String defaultUrl) {
        if (CACHE_URL.containsKey(key)) {
            return CACHE_URL.get(key);
        }

        if (StringUtils.isNotBlank(key)) {
            Matcher matcher = URL_NAME_PATTERN.matcher(key);

            if (matcher.find()) {
                do {
                    String temp = matcher.group();
                    String str = matcher.group().replace("{", "").replace("}", "");

                    key = key.replace(temp, getDefaultUrl(str, defaultUrl));
                } while (matcher.find());
            } else {
                return getDefaultUrl(key, defaultUrl);
            }

            return key;
        } else {
            return getDefaultUrl(key, defaultUrl);
        }
    }

    /**
     * 获取url
     *
     * @param key        key值
     * @param defaultUrl 默认值
     * @return 地址
     */
    private static String getDefaultUrl(String key, String defaultUrl) {
        if (key == null) {
            return defaultUrl;
        }

        String url = CACHE_URL.get(key);

        if (StringUtils.isBlank(url)) {
            return defaultUrl;
        } else {
            return url;
        }
    }

    /**
     * 获取url
     *
     * @param url url
     * @return 全地址
     */
    public static String getFullUrl(String url) {
        String baseUrl = CACHE_URL.get("file");

        if (StringUtils.isBlank(url)) {
            return null;
        } else {
            return baseUrl + url;
        }
    }
}
